//
//  AlertPwdSmsCodeController.m
//  XiaoMiao
//
//  Created by jinglian on 2018/2/10.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "AlertPwdSmsCodeController.h"
#import "SuccessToOriginController.h"

@interface AlertPwdSmsCodeController () {
    NSString *oldPwd_;
    NSString *refreshPwd_;
}

@property (weak, nonatomic) IBOutlet UILabel *codeSendedLabel;

@property (weak, nonatomic) IBOutlet UITextField *codeTextField;

@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@end

@implementation AlertPwdSmsCodeController

// 修改密码(已登陆)
- (instancetype)initWithOldPwd:(NSString *)oldPwd refreshPwd:(NSString *)refreshPwd {
    if (self = [super init]) {
        oldPwd_ = oldPwd;
        refreshPwd_ = refreshPwd;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改密码";
    
    LoginInfoModel *infoModel = [LoginInfoModel getLoginInfo];
    
    NSString *colorStr = [NSString stringWithFormat:@"%@ %@", infoModel.prix, infoModel.phone];
    NSString *totalStr = [NSString stringWithFormat:@"已发送验证码短信到 %@", colorStr];
    self.codeSendedLabel.attributedText = [NSMutableAttributedString app_setAttributedColor:kMainBlueColor totalStr:totalStr colorStr:colorStr];
    
    [self.confirmButton addTarget:self action:@selector(btnDidClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnDidClick {
    if ([NSString app_isBlankString:self.codeTextField.text]) {
        [KLProgressHUD showPromptHUDWithMessage:@"请输入验证码"];
        return;
    }
    
    [self request_updatePwd];
}

- (void)request_updatePwd {
    KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:kLoadingMsg];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"oldPassWord"] = [NSString app_md5:oldPwd_];
    param[@"passWord"] = [NSString app_md5:refreshPwd_];
    param[@"phone"] = [AppGlobal getLoginAccount];
    param[@"smsCode"] = self.codeTextField.text;
    
    WEAKSELF
    [AppNetwork postWithUrl:kUpdatePwdPath params:param success:^(id response) {
        progressHUD.hidden = YES;
        
        [AppGlobal updateLoginPwd:refreshPwd_];
        
        [KLProgressHUD showSuccessHUDWithSuperView:self.view message:@"修改成功" isHudEnable:YES andTime:1.0 completed:^{
            SuccessToOriginController *success = [[SuccessToOriginController alloc] init];
            [success setNavTitle:@"修改成功" desTitle:@"修改成功" btnTitle:@"回到个人中心"];
            [weakSelf.navigationController pushViewController:success animated:YES];
            
            [success setBtnClickHandle:^(SuccessToOriginController *btnVC) {
                [btnVC.navigationController popToRootViewControllerAnimated:YES];
            }];
        }];

    } fail:^(AppError *error) {
        progressHUD.hidden = YES;
        [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
    }];
}

@end
